package cn.itrip.auth.controller;

import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Super God Jie
 * @version 1.0  2022-04-14 20:33
 */
@RestController
@RequestMapping("/api")
public class LoginController {

	@Resource
	private UserService userService;

	@PostMapping("/dologin")
	public cn.itrip.beans.dto.Dto doLogin(String name, String password, HttpServletRequest request){
		return userService.login(name,password,request.getHeader("User-Agent"));
	}

	@GetMapping("/ckusr")
	public cn.itrip.beans.dto.Dto ckusr(String name){
		return userService.checkUserCode(name);
	}

	@PostMapping("/doregister")
	public cn.itrip.beans.dto.Dto doregister(@RequestBody ItripUserVO itripUserVO){
		return userService.register(itripUserVO,"mail");
	}

	@PostMapping("/registerbyphone")
	public cn.itrip.beans.dto.Dto registerbyphone(@RequestBody ItripUserVO itripUserVO){
		try{
			return userService.register(itripUserVO,"phone");
		}catch (Exception e){
			return DtoUtil.returnFail("注册失败","10002");
		}
	}

	@PutMapping("/activate")
	public Dto activate(String user, String code){
		return userService.active(user,code);
	}
}